package com.blue.base.model;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class ResultDTO<T> implements Serializable {
    public static final Integer RESULT = 1;
    private Long timestamp;
    @JSONField(
            format = "yyyy-MM-dd HH:mm:ss:SSS"
    )
    private Date dateTime;
    private Long timeConsuming;
    private Integer status;
    private String error;
    private String message;
    private String path;
    private String dataVersion;
    private T data;
    private String poweredBy;

    public String toString() {
        return "Result{status=" + this.status + ", msg='" + this.message + '\'' + ", data=" + this.data + '}';
    }

    public ResultDTO() {
        this.timestamp = System.currentTimeMillis();
        this.dateTime = new Date(this.timestamp);
        this.dataVersion = "1.0";
        this.poweredBy = "nothignull@datadriver";
        this.status = 0;
    }

    public ResultDTO(T data) {
        this();
        this.data = data;
    }

    public ResultDTO(Integer result, String successMsg, String failMsg) {
        if (result == RESULT) {
            this.setStatus(200);
            this.setMessage(successMsg);
        } else {
            this.setStatus(0);
            this.setMessage(failMsg);
        }

    }

    public void doSuccess(String msg) {
        this.setStatus(200);
        this.setMessage(msg);
    }

    public void doError(String msg) {
        this.setStatus(0);
        this.setMessage(msg);
    }


}
